

#include "importBitmapButton.h"
#include "renderer.h"
#include "input.h"
#include "drawingWidget.h"

ImportBitmapButton::ImportBitmapButton(){

buttonProperty="NULL";
listDisplayMode=3;
color=Vector4f(0.5,0.5,0.5,1.0);

listWidth=200;
listColor=Vector4f(0.4,0.4,0.4,1.0);
listShader="color";
}

ImportBitmapButton::~ImportBitmapButton(){}

void ImportBitmapButton::mouseDrag(){}

void ImportBitmapButton::clickedLeft(){

    input->deselectButtons(0);
    bDrawName=true;

    input->getAllImages();

    Actor * myParent=parent;

    LoadButton::clickedLeft();

    parent=myParent;

    bDrawName=false;
}

void ImportBitmapButton::trigger(Actor *other){

ListButton::trigger(other);
if (other==scrollBar)
    return;

DrawingWidget* dW=dynamic_cast<DrawingWidget*>(parent);

if (dW && dW->brush && dW->brush->drawing){
    //clean up particleSystem
    dW->brush->drawing->drawType=4;
    for (int i=dW->brush->drawing->particles.size()-1;i>-1;i--)
        dW->brush->drawing->deleteParticle(i);
    loadFile(other->name);
    dW->brush->drawing->sceneShaderID="simpleLight";
    }
input->deselectButtons(0);
}

void ImportBitmapButton::assembleLoadList(){

    parent=NULL;
    for (unsigned int i=0;i<input->userImages.size();i++)
      {
        listType.push_back("12AssignButton");
        listName.push_back(input->userImages[i]);
        listProp.push_back("NULL");
        listIcon.push_back("icon_flat");
      }
}

void ImportBitmapButton::placeScrollBar(){

    scrollBar->location.x=location.x-17;
    scrollBar->location.y=location.y- scale.y -( maxListItems * listButton[0]->scale.y );
    scrollBar->bFlipValue=true;

}

void ImportBitmapButton::loadFile(string filename){


cout << "loading image " << filename << endl;
string fullName="resources/images/";
fullName.append(filename);
FIBITMAP * myBitmap = FreeImage_Load(FIF_TARGA,fullName.c_str(),0);
int imageWidth=FreeImage_GetWidth(myBitmap);
int imageHeight=FreeImage_GetHeight(myBitmap);
//never used:
//int channels=FreeImage_GetBPP(myBitmap)/8;

//aquire brush if existing
if (!DrawingWidget::brush)
  {
  FreeImage_Unload(myBitmap);
  cout << "not connected to Brush!" << endl;
  return;
  }


  Vector4f influences;

  //go through all of the image
  for (int h=0;h<imageHeight;h++)
    {
    //reset after every line
    influences=Vector4f(0,0,0,0);

    for (int w=0;w<imageWidth;w++)
      {
          RGBQUAD *myColor=new RGBQUAD;
          //Gathering influences
          if (w>=1){
              FreeImage_GetPixelColor(myBitmap,w-1,h,myColor);
              influences.x=(float)myColor->rgbReserved;
          }

          if (h>=1){
             FreeImage_GetPixelColor(myBitmap,w,h-1,myColor);
             influences.y=(float)myColor->rgbReserved;
          }

          if (w<imageWidth-1){
             FreeImage_GetPixelColor(myBitmap,w+1,h,myColor);
             influences.z=(float)myColor->rgbReserved;
          }

          if (h<imageHeight-1){
             FreeImage_GetPixelColor(myBitmap,w,h+1,myColor);
             influences.w=(float)myColor->rgbReserved;
          }
         //given that we won't have many instances where we jump by more than 16 points per pixel...
         Vector3f xNormal=Vector3f(1.0f, 0.0f  ,influences.x -influences.z );
         Vector3f yNormal=Vector3f(0.0f, 1.0f,  influences.w - influences.y  );
         xNormal.normalize();
         yNormal.normalize();

         Vector3f zNormal =xNormal.crossProduct(yNormal);
         zNormal.normalize();

         Vector3f xyzNormal=zNormal;
         //xyzNormal.normalize();

         //Vector3f xyNormal=Vector3f(influences.x -influences.z, influences.y -influences.w  ,0.0f );
         //if (xyNormal.length()>0)
            //xyNormal.normalize();

         //Vector3f xyzNormal=xyNormal+Vector3f(0.0,0.0,0.2);
         //xyzNormal.normalize();


         DrawingWidget::brush->pNormal=xyzNormal;


          FreeImage_GetPixelColor(myBitmap,w,h,myColor);
          DrawingWidget::brush->color.r=(float)myColor->rgbRed/255.0f;
          DrawingWidget::brush->color.g=(float)myColor->rgbGreen/255.0f;
          DrawingWidget::brush->color.b=(float)myColor->rgbBlue/255.0f;




          DrawingWidget::brush->location.x=DrawingWidget::brush->drawing->location.x+ 10 * (float) (imageWidth/2.0 - w)/imageWidth;
          if (input->bShiftDown)
            {
            DrawingWidget::brush->location.y=DrawingWidget::brush->drawing->location.y+ 10 *  (float)h/imageHeight;
            DrawingWidget::brush->location.z=DrawingWidget::brush->drawing->location.z+(float)myColor->rgbReserved/255.0f;
            }
          else
            {
            DrawingWidget::brush->location.z=DrawingWidget::brush->drawing->location.z+(float)h/imageHeight;
            DrawingWidget::brush->location.y=DrawingWidget::brush->drawing->location.y+(float)myColor->rgbReserved/255.0f;

            DrawingWidget::brush->location.x=DrawingWidget::brush->location.x * 10;
            DrawingWidget::brush->location.y=DrawingWidget::brush->location.z * 10;
            }
          if ((int)myColor->rgbReserved!=0){
            input->mouseVector=Vector3f(0.1,0.1,0.1);   //HACK so that we actually paint something!

            renderer->setupCamera(false);
            renderer->drawActor(DrawingWidget::brush);

            DrawingWidget::brush->paint();
            }
      }
    }

  FreeImage_Unload(myBitmap);
}

void ImportBitmapButton::create(){renderer->addButton(this);}
